import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Qt5Compat.GraphicalEffects
import FluentUI

import MyApp

FluRectangle {
    property var device
    Layout.fillHeight: true
    color: {
        if (Window.active) {
            return FluTheme.frameActiveColor
        }
        return FluTheme.frameColor
    }
    radius: [4, 4, 4, 4]

    ColumnLayout {
        anchors.fill: parent
        FluText {
            Layout.margins: 8
            text: "历史"
            font.pixelSize: 16
            font.bold: true
        }
        ListView {
            id: result_list
            clip: true
            spacing: 5
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.leftMargin: 8
            model: device.results
            ScrollBar.vertical: FluScrollBar {// width: 10
            }
            delegate: Item {
                height: 60
                width: ListView.view.width-6
                FluButton {
                    anchors.fill: parent
                    anchors.rightMargin: 6
                    ColumnLayout {
                        anchors.fill: parent
                        anchors.margins: 10
                        spacing: 8
                        RowLayout {
                            FluText {
                                text: (index + 1) + "."
                                font.pixelSize: 14
                            }
                            FluText {
                                text: {
                                    switch (modelData.task_type) {
                                    case Types.GET_EIS:
                                        return "阻抗图谱"
                                    case Types.GET_SINGLE:
                                        return "单点阻抗"
                                    default:
                                        return "未知类型"
                                    }
                                }
                                font.bold: true
                                font.pixelSize: 14
                            }
                            Item {
                                Layout.fillWidth: true
                            }
                            FluText {
                                text: {
                                    switch (modelData.driving_current) {
                                    case Types.AMPL_1A:
                                        return "1A"
                                    case Types.AMPL_A15:
                                        return "0.15A"
                                    case Types.AMPL_A25:
                                        return "0.25A"
                                    case Types.AMPL_A5:
                                        return "0.5A"
                                    case Types.AMPL_A8:
                                        return "0.8A"
                                    }
                                }
                                font.pixelSize: 14
                            }
                        }
                        RowLayout {
                            FluText {
                                // 格式化为 YYYY-MM-DD hh:mm:ss
                                text: "频点数：" + modelData.points.length
                                font.pixelSize: 14
                            }
                            Item {
                                Layout.fillWidth: true
                            }
                            FluText {
                                // 格式化为 YYYY-MM-DD hh:mm:ss
                                text: modelData.create_time.toLocaleString(
                                          Qt.LocalTime, "yyyy-MM-dd hh:mm:ss")
                                font.pixelSize: 14
                            }
                        }
                    }
                    onClicked: {
                        client.aquireResult(device.device_id, index)
                        // FluRouter.navigate("/results", argument = modelData)
                    }
                    Connections {
                        target: client
                        function onResultReceived(deviceId, resultIndex) {
                            if (deviceId === device.device_id
                                    && resultIndex === index) {
                                FluRouter.navigate("/results",
                                                   argument = modelData)
                            }
                        }
                    }
                }
            }
        }
    }
}
